/*
 * @(#)Powell.java        1.0 2000/06/26
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.model.algorithm;

import xfuzzy.xfsl.model.*;
import xfuzzy.lang.*;
import xfuzzy.xfds.XfdsDataSet;

/**
 * Algoritmo de Powell basado en b�squeda de direcciones conjugadas sin 
 * utilizar el gradiente.
 * 
 * @author Francisco Jos� Moreno Velo
 * 
 * @see "Brent, R.P., Algorithms for Minimization Without derivatives, 
 * Prentice-Hall, 1973."
 *
 */
public class Powell extends XfslAlgorithm {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Factor de tolerancia en la b�squeda lineal
	 */
	private double tol;
	
	/**
	 * L�mite de iteraciones en la b�squeda lineal
	 */
	private int limit;
	
	/**
	 * N�mero de iteraciones del algoritmo
	 */
	private int iter;
	
	/**
	 * Conjunto de direcciones conjugadas encontradas
	 */
	private double[][] dirset;
	
	/**
	 * Evaluaci�n de la �ltima iteraci�n
	 */
	private XfslEvaluation lasteval;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public Powell() {
		this.tol = 0.1;
		this.limit = 10;
		this.iter = 0;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Devuelve el c�digo de identificaci�n del algoritmo
	 */
	public int getCode() {
		return POWELL;
	}

	/**
	 * Actualiza los par�metros de configuraci�n del algoritmo
	 */
	public void setParameters(double[] param) throws XflException {
		if(param.length != 2) throw new XflException(26);
		tol = test(param[0], REDUCE);
		limit = (int) test(param[1], INTEGER);
	}

	/**
	 * Obtiene los par�metros de configuraci�n del algoritmo
	 */
	public XfslAlgorithmParam[] getParams() {
		XfslAlgorithmParam[] pp = new XfslAlgorithmParam[2];
		pp[0] = new XfslAlgorithmParam(tol, 0.1, REDUCE, "Line-search Tolerance");
		pp[1] = new XfslAlgorithmParam(limit, 10, INTEGER, "Search Iteration Limit");
		return pp;
	}

	/**
	 * Ejecuta una iteraci�n del algoritmo 
	 */
	public XfslEvaluation iteration(Specification spec, XfdsDataSet pattern,
			XfslErrorFunction ef) throws XflException {
		Parameter[] param = spec.getAdjustable();
		if(init) {
			init = false;
			iter = 0;
			dirset = new double[param.length][param.length];
			for(int i=0; i<dirset.length; i++) dirset[i][i] = 1.0;
			lasteval = ef.evaluate(spec,pattern,1.0);
		}

		iter++;
		if(iter == param.length) {
			iter = 0;
			for(int i=0; i<dirset.length; i++) dirset[i][i] = 1.0;
			for(int i=0; i<param.length-1; i++)
				for(int j=i+1; j<param.length; j++) {
					dirset[i][j] = 0.0;
					dirset[j][i] = 0.0;
				}
		}

		OptimizingFunction function = new OptimizingFunction(spec,pattern,ef);
		double[] pt = new double[param.length];
		for(int i=0; i<param.length; i++) pt[i] = param[i].value;

		XfslEvaluation neweval = lasteval;
		for(int i=0; i<dirset.length; i++) 
			neweval = function.linmin(dirset[i],neweval,tol,limit);
		for(int i=0; i<param.length-1; i++)
			for(int j=0; j<param.length; j++)
				dirset[i][j] = dirset[i+1][j];
		for(int i=0; i<param.length; i++)
			dirset[param.length-1][i] = param[i].value - pt[i];

		neweval = function.linmin(dirset[param.length-1],neweval,tol,limit);
		neweval.var = (lasteval.error - neweval.error)/lasteval.error;
		lasteval = neweval;

		return lasteval;
	}
}

